Sms.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. /**
  3. * NiuShop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. */
  9. namespace addon\niusms\shop\controller;
  10. use addon\niusms\model\Config as ConfigModel;
  11. use addon\niusms\model\Order as SmsOrderModel;
  12. use addon\niusms\model\Sms as SmsModel;
  13. use app\shop\controller\BaseShop;
  14. use app\model\message\Message;
  15. use think\facade\Cache;
  16. /**
  17. * 牛云云短信 控制器
  18. */
  19. class Sms extends BaseShop
  20. {
  21. protected $replace = [
  22. 'NIU_SMS_CSS' => __ROOT__ . '/addon/niusms/shop/view/public/css',
  23. 'NIU_SMS_JS' => __ROOT__ . '/addon/niusms/shop/view/public/js',
  24. 'NIU_SMS_IMG' => __ROOT__ . '/addon/niusms/shop/view/public/img',
  25. ];
  26. public function index()
  27. {
  28. $buy = input("buy", 0);
  29. $return = input("return", 0);
  30. $sms_model = new SmsModel();
  31. $config_model = new ConfigModel();
  32. $sms_config = $config_model->getSmsConfig($this->site_id, $this->app_module)[ 'data' ];
  33. if (!empty($sms_config[ 'value' ][ 'username' ])) {
  34. if ($return == 1) {
  35. return $this->returnUrl();
  36. } elseif ($buy == 1) {
  37. // 短信充值
  38. $sms_package_list = $sms_model->getSmsPackageList();
  39. $this->assign("sms_package_list", $sms_package_list[ 'data' ]);
  40. return $this->fetch("sms/buy");
  41. } else {
  42. // 个人主页
  43. $account_info = $sms_model->getChildAccountInfo([
  44. 'username' => input('username', $sms_config[ 'value' ][ 'username' ]),//子账户用户名
  45. ]);
  46. if (!empty($account_info[ 'data' ])) {
  47. $this->assign("account_info", $account_info[ 'data' ]);
  48. if (!isset($sms_config[ 'value' ][ 'signature' ])) {
  49. $sms_config[ 'value' ][ 'signature' ] = '';
  50. }
  51. $this->assign("sms_config", $sms_config);
  52. $signature_status = $sms_model->querySignature($this->site_id, $this->app_module);
  53. $this->assign("signature_status", $signature_status);
  54. return $this->fetch("sms/index");
  55. } else {
  56. $this->error('您还没有账号,即将跳转注册页面', addon_url('niusms://shop/sms/register'), '', '5');
  57. // $this->redirect(addon_url("niusms://shop/sms/login"));
  58. }
  59. }
  60. } else {
  61. // 未注册
  62. $this->error('您还没有账号,即将跳转注册页面', addon_url('niusms://shop/sms/register'), '', '5');
  63. // $this->redirect(addon_url("niusms://shop/sms/login"));
  64. }
  65. }
  66. /**
  67. * 注册
  68. * @return array|mixed
  69. */
  70. public function register()
  71. {
  72. if (request()->isAjax()) {
  73. $sms_model = new SmsModel();
  74. $res = $sms_model->createChildAccount([
  75. 'username' => input('username', ''),//子账户用户名
  76. 'password' => input('password', ''),//子账户密码
  77. 'company' => input('company', ''),//子账户公司名
  78. 'mobiles' => input('mobiles', ''),//子账户手机号
  79. 'domain' => input('domain', ''),//手机端网址
  80. 'key' => input('key', ''),//key
  81. 'code' => input('code', ''),//动态码
  82. ], $this->site_id, $this->app_module);
  83. return $res;
  84. } else {
  85. $captcha = $this->captcha();
  86. $this->assign("captcha", $captcha[ 'data' ]);
  87. return $this->fetch("sms/register");
  88. }
  89. }
  90. /**
  91. * 登录
  92. * @return array|mixed
  93. */
  94. public function login()
  95. {
  96. if (request()->isAjax()) {
  97. $sms_model = new SmsModel();
  98. $res = $sms_model->loginAccount([
  99. 'username' => input('username', ''),//子账户用户名
  100. 'password' => input('password', ''),//子账户密码
  101. ], $this->site_id, $this->app_module);
  102. return $res;
  103. } else {
  104. return $this->fetch("sms/login");
  105. }
  106. }
  107. /**
  108. * 重置密码
  109. * @return array|mixed
  110. */
  111. public function forget()
  112. {
  113. if (request()->isAjax()) {
  114. $data = [
  115. 'key' => input('key', ''),//key
  116. 'code' => input('code', ''),//动态码
  117. 'mobile' => input('mobiles', ''),//手机号
  118. ];
  119. $sms_model = new SmsModel();
  120. $res = $sms_model->resetPassword($data);
  121. return $res;
  122. } else {
  123. $captcha = $this->captcha();
  124. $this->assign("captcha", $captcha[ 'data' ]);
  125. return $this->fetch("sms/forget");
  126. }
  127. }
  128. /**
  129. * 获取验证码
  130. * @return mixed
  131. */
  132. public function captcha()
  133. {
  134. $sms_model = new SmsModel();
  135. $res = $sms_model->captcha();
  136. return $res;
  137. }
  138. /**
  139. * 获取手机动态码
  140. * @return mixed
  141. */
  142. public function mobileCode()
  143. {
  144. if (request()->isAjax()) {
  145. $data = [
  146. 'captcha_id' => input('captcha_id', ''),//动态码Id
  147. 'captcha_code' => input('captcha_code', ''),//动态码
  148. 'mobile' => input('mobiles', ''),//手机号
  149. ];
  150. $sms_model = new SmsModel();
  151. $res = $sms_model->mobileCode($data);
  152. return $res;
  153. }
  154. }
  155. /**
  156. * 编辑短信签名
  157. * @return array|mixed
  158. */
  159. public function addChildSignature()
  160. {
  161. if (request()->isAjax()) {
  162. $sms_model = new SmsModel();
  163. $config_model = new ConfigModel();
  164. $sms_config = $config_model->getSmsConfig($this->site_id, $this->app_module);
  165. $sms_config = $sms_config[ 'data' ][ 'value' ];
  166. $img = input('default_goods_img', '');
  167. if (strpos($img, 'http://') === false || strpos($img, 'https://') === false) {
  168. $img = $_SERVER[ 'HTTP_HOST' ] . $img;
  169. }
  170. $res = $sms_model->addChildSignature([
  171. 'username' => $sms_config[ 'username' ],
  172. 'signature' => input('signature', ''),//短信签名
  173. 'remark' => '签名类型:' . input('type', '') . '。' . '截图链接:' . $img . '。' . '备注:' . input('remark', '') . '。'
  174. ], $this->site_id, $this->app_module);
  175. return $res;
  176. }
  177. }
  178. /**
  179. * 订单计算
  180. * @return mixed
  181. */
  182. public function calculate()
  183. {
  184. if (request()->isAjax()) {
  185. $sms_model = new SmsOrderModel();
  186. $res = $sms_model->calculate([
  187. 'package_id' => input('package_id', 0),
  188. 'is_invoice' => input('is_invoice', 0),
  189. 'invoice_id' => input('invoice_id', 0)
  190. ]
  191. );
  192. return $res;
  193. }
  194. }
  195. /**
  196. * 创建短信订单
  197. * @return mixed
  198. */
  199. public function createSmsOrder()
  200. {
  201. if (request()->isAjax()) {
  202. $sms_order_model = new SmsOrderModel();
  203. $config_model = new ConfigModel();
  204. $sms_config = $config_model->getSmsConfig($this->site_id, $this->app_module);
  205. $sms_config = $sms_config[ 'data' ][ 'value' ];
  206. $res = $sms_order_model->createSmsOrder([
  207. 'username' => $sms_config[ 'username' ],
  208. 'package_id' => input('package_id', 0),
  209. 'is_invoice' => input('is_invoice', 0),
  210. 'consigner' => input('name', ''),
  211. 'mobile' => input('mobile', ''),
  212. 'address' => input('address', ''),
  213. 'zip_code' => input('zip_code', ''),
  214. 'invoice_title' => input('invoice_title', ''),
  215. 'invoice_id' => input('invoice_id', 0),
  216. 'invoice_number' => input('invoice_number', ''),
  217. 'invoice_content' => input('invoice_content', '')
  218. ]
  219. );
  220. // if ($res[ 'code' ] >= 0) {
  221. //添加关闭短信充值订单事件
  222. // $cron = new Cron();
  223. // $execute_time = ( time() + ( 60 * 15 ) );
  224. // $cron->addCron(1, 0, "关闭短信充值订单", "CloseSmsPayment", $execute_time, $res[ 'data' ][ 'out_trade_no' ]);
  225. // event('CloseSmsPayment', [ 'relate_id' => $res[ 'data' ][ 'out_trade_no' ] ], true);
  226. // }
  227. return $res;
  228. }
  229. }
  230. /**
  231. * 调用支付宝支付
  232. */
  233. public function payment()
  234. {
  235. $return_url = __ROOT__ . '/index.php/niusms/shop/sms/index?return=1';
  236. $sms_model = new SmsOrderModel();
  237. $res = $sms_model->payment([
  238. 'out_trade_no' => input('out_trade_no', '159618756248971000'),//子账户用户名
  239. 'return_url' => $return_url,
  240. ]
  241. );
  242. echo $res;
  243. }
  244. /**
  245. * 支付同步回调
  246. * @return mixed
  247. */
  248. public function returnUrl()
  249. {
  250. $out_trade_no = input("out_trade_no", "");
  251. $this->assign("out_trade_no", $out_trade_no);
  252. $sms_model = new SmsOrderModel();
  253. $order_info = $sms_model->getSmsOrderInfo([ 'out_trade_no' => $out_trade_no ]);
  254. $order_info = $order_info[ 'data' ];
  255. $this->assign("order_info", $order_info);
  256. return $this->fetch("sms/pay_result", [], $this->replace);
  257. }
  258. /**
  259. * 获取短信模板分页列表
  260. * @return array
  261. */
  262. public function getSmsTemplatePageList()
  263. {
  264. if (request()->isAjax()) {
  265. $page = input('page', 1);
  266. $page_size = input('page_size', PAGE_LIST_ROWS);
  267. $config_model = new ConfigModel();
  268. $sms_config = $config_model->getSmsConfig($this->site_id, $this->app_module)[ 'data' ][ 'value' ];
  269. $sms_model = new SmsModel();
  270. // [['mobile','=',$sms_config['mobile']]]
  271. $list = $sms_model->getSmsTemplatePageList($this->site_id, [], $page, $page_size);
  272. foreach ($list[ 'data' ][ 'list' ] as $k => $v) {
  273. $audit_status = $sms_model->getAuditStatus();
  274. $list[ 'data' ][ 'list' ][ $k ][ 'audit_status_name' ] = $audit_status [ $v[ 'audit_status' ] ];
  275. }
  276. return $list;
  277. }
  278. }
  279. /**
  280. * 开关短信
  281. * @return array
  282. */
  283. public function modifyConfigIsUse()
  284. {
  285. $config_model = new ConfigModel();
  286. $is_use = input("is_use", 0);
  287. $result = $config_model->modifyConfigIsUse($is_use, $this->site_id, $this->app_module);
  288. return $result;
  289. }
  290. /**
  291. * 获取短信充值订单列表
  292. * @return mixed
  293. */
  294. public function getSmsOrderList()
  295. {
  296. if (request()->isAjax()) {
  297. $page = input('page', 1);
  298. $page_size = input('page_size', PAGE_LIST_ROWS);
  299. $config_model = new ConfigModel();
  300. $sms_config = $config_model->getSmsConfig($this->site_id, $this->app_module);
  301. $sms_config = $sms_config[ 'data' ][ 'value' ];
  302. $sms_model = new SmsOrderModel();
  303. $order_list = $sms_model->getSmsOrderList([ 'page' => $page, 'page_size' => $page_size, 'username' => $sms_config[ 'username' ] ]);
  304. return $order_list;
  305. }
  306. }
  307. /**
  308. * 签名管理
  309. */
  310. public function signList()
  311. {
  312. //短信配置
  313. $config_model = new ConfigModel();
  314. $sms_config = $config_model->getSmsConfig($this->site_id, $this->app_module);
  315. $sms_config = $sms_config[ 'data' ][ 'value' ];
  316. if (request()->isAjax()) {
  317. $page = input('page', 1);
  318. $page_size = input('page_size', PAGE_LIST_ROWS);
  319. $signature = input('signature', '');
  320. $status = input('status', '');//审核状态 1 - 待审核,2 - 审核通过,3 - 审核不通过;
  321. $data = [
  322. 'page' => $page,
  323. 'page_size' => $page_size,
  324. 'username' => $sms_config[ 'username' ],
  325. 'signature' => $signature,
  326. 'status' => $status
  327. ];
  328. $sms_model = new SmsModel();
  329. $list = $sms_model->getChildSignatureList($data);
  330. return $list;
  331. } else {
  332. $this->assign('sms_config', $sms_config);
  333. return $this->fetch('sms/sign_list');
  334. }
  335. }
  336. /**
  337. * 更换签名
  338. */
  339. public function changeSignature()
  340. {
  341. if (request()->isAjax()) {
  342. $signature = input('signature', '');
  343. //短信配置
  344. $config_model = new ConfigModel();
  345. $sms_config_result = $config_model->getSmsConfig($this->site_id, $this->app_module);
  346. $sms_config = $sms_config_result[ 'data' ][ 'value' ];
  347. $sms_config[ 'signature' ] = $signature;
  348. $res = $config_model->setSmsConfig($sms_config, $sms_config_result[ 'data' ][ 'is_use' ], $this->site_id, $this->app_module);
  349. return $res;
  350. }
  351. }
  352. /**
  353. * @return mixed
  354. * 修改绑定手机
  355. */
  356. public function updateNiusmsTel()
  357. {
  358. $mobiles = input('tel', "");
  359. $username = input('username', "");
  360. $sms_model = new SmsModel();
  361. $res = $sms_model->updateNiusmsTel($mobiles, $username);
  362. return $res;
  363. }
  364. /**
  365. * 发送手机动态码
  366. */
  367. public function getMobleCode()
  368. {
  369. $mobile = input('mobile', "");
  370. if ($mobile == "") {
  371. $this->error("手机号不可为空!");
  372. }
  373. $code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT);// 生成4位随机数,左侧补0
  374. $message_model = new Message();
  375. $res = $message_model->sendMessage([ "mobile" => $mobile, "site_id" => $this->site_id, "support_type" => [ 'sms' ], "code" => $code, "keywords" => "LOGIN_CODE" ]);
  376. if ($res[ "code" ] >= 0) {
  377. //将验证码存入缓存
  378. $key = 'login_mobile_code_' . md5($mobile . $code);
  379. Cache::tag("login_mobile_code")->set($key, [ 'mobile' => $mobile, 'code' => $code ], 600);
  380. $res[ 'key' ] = $key;
  381. return $res;
  382. } else {
  383. return $res;
  384. }
  385. }
  386. public function codeValidate()
  387. {
  388. $code = input('code', "");
  389. $code_mobile = input('code_mobile', "");
  390. $key = input('key', "");
  391. $verify_data = Cache::get($key);
  392. if ($verify_data[ "mobile" ] == $code_mobile && $verify_data[ "code" ] == $code) {
  393. return 1;
  394. }
  395. return 0;
  396. }
  397. }