Sms.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\system\sms;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\system\config\ConfigValueRepository;
  14. use app\common\repositories\system\sms\SmsRecordRepository;
  15. use app\validate\admin\SmsRegisterValidate;
  16. use crmeb\services\YunxinSmsService;
  17. use think\App;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. use think\facade\Cache;
  22. /**
  23. * Class Sms
  24. * @package app\controller\admin\system\sms
  25. * @author xaboy
  26. * @day 2020-05-18
  27. */
  28. class Sms extends BaseController
  29. {
  30. /**
  31. * @var YunxinSmsService
  32. */
  33. protected $service;
  34. /**
  35. * Sms constructor.
  36. * @param App $app
  37. */
  38. public function __construct(App $app)
  39. {
  40. parent::__construct($app);
  41. $this->service = YunxinSmsService::create();
  42. }
  43. /**
  44. * @return mixed
  45. * @author xaboy
  46. * @day 2020-05-18
  47. */
  48. public function captcha()
  49. {
  50. $phone = request()->param('phone');
  51. if (!$phone)
  52. return app('json')->fail('请输入手机号');
  53. if (!preg_match('/^1[3456789]{1}\d{9}$/', $phone))
  54. return app('json')->fail('请输入正确的手机号');
  55. $res = $this->service->captcha($phone);
  56. if (!isset($res['status']) && $res['status'] !== 200)
  57. return app('json')->fail($res['data']['message'] ?? $res['msg'] ?? '发送失败');
  58. return app('json')->success($res['data']['message'] ?? $res['msg'] ?? '发送成功');
  59. }
  60. /**
  61. * @param SmsRegisterValidate $validate
  62. * @return mixed
  63. * @author xaboy
  64. * @day 2020-05-18
  65. */
  66. public function save(SmsRegisterValidate $validate)
  67. {
  68. $data = $this->request->params(['account', 'password', 'phone', 'code', 'url', 'sign']);
  69. $validate->check($data);
  70. $data['password'] = md5($data['password']);
  71. $res = $this->service->registerData($data);
  72. if ($res['status'] == 400) return app('json')->fail('短信平台:' . $res['msg']);
  73. $this->service->setConfig($data['account'], $data['password']);
  74. return app('json')->success('短信平台:' . $res['msg']);
  75. }
  76. /**
  77. * @param SmsRegisterValidate $validate
  78. * @param ConfigValueRepository $repository
  79. * @return mixed
  80. * @author xaboy
  81. * @day 2020-05-18
  82. */
  83. public function save_basics(SmsRegisterValidate $validate, ConfigValueRepository $repository)
  84. {
  85. $data = $this->request->params([
  86. 'account', 'password'
  87. ]);
  88. $validate->isLogin()->check($data);
  89. $this->service->setConfig($data['account'], md5($data['password']));
  90. //添加公共短信模板
  91. $templateList = $this->service->publictemp([]);
  92. if ($templateList['status'] != 400) {
  93. $repository->setFormData(['sms_account' => $data['account'], 'sms_token' => md5($data['password'])], 0);
  94. return app('json')->success('登录成功');
  95. } else {
  96. return app('json')->fail('账号或密码错误');
  97. }
  98. }
  99. /**
  100. * @return mixed
  101. * @author xaboy
  102. * @day 2020-05-18
  103. */
  104. public function is_login()
  105. {
  106. if ($sms_info = $this->service->account()) {
  107. return app('json')->success(['status' => true, 'info' => $sms_info]);
  108. } else {
  109. return app('json')->success(['status' => false]);
  110. }
  111. }
  112. /**
  113. * @param SmsRecordRepository $repository
  114. * @return mixed
  115. * @throws DataNotFoundException
  116. * @throws DbException
  117. * @throws ModelNotFoundException
  118. * @author xaboy
  119. * @day 2020-05-18
  120. */
  121. public function record(SmsRecordRepository $repository)
  122. {
  123. [$page, $limit] = $this->getPage();
  124. $where = $this->request->params(['type',0]);
  125. return app('json')->success($repository->getList($where, $page, $limit));
  126. }
  127. /**
  128. * @param SmsRecordRepository $repository
  129. * @return mixed
  130. * @author xaboy
  131. * @day 2020-05-18
  132. */
  133. public function data(SmsRecordRepository $repository)
  134. {
  135. $countInfo = $this->service->count();
  136. if ($countInfo['status'] == 400) {
  137. $info['number'] = 0;
  138. $info['total_number'] = 0;
  139. } else {
  140. $info['number'] = $countInfo['data']['number'];
  141. $info['total_number'] = $countInfo['data']['send_total'];
  142. }
  143. $info['record_number'] = $repository->count();
  144. $info['sms_account'] = $this->service->account();
  145. return app('json')->success($info);
  146. }
  147. /**
  148. * @param ConfigValueRepository $repository
  149. * @return mixed
  150. * @throws DbException
  151. * @author xaboy
  152. * @day 2020-05-18
  153. */
  154. public function logout(ConfigValueRepository $repository)
  155. {
  156. Cache::delete('sms_account');
  157. Cache::delete('serve_account');
  158. $repository->clear(['sms_account', 'sms_token','serve_account','serve_token'], 0);
  159. return app('json')->success('退出成功');
  160. }
  161. /**
  162. * 修改密码
  163. * @Author:Qinii
  164. * @Date: 2020/9/2
  165. * @return mixed
  166. */
  167. public function changePassword()
  168. {
  169. $data = $this->request->params(['password','phone','code']);
  170. if(empty($data['password']))
  171. return app('json')->fail('密码不能为空');
  172. $data['password'] = md5($data['password']);
  173. $res = $this->service->smsChange($data);
  174. if ($res['status'] == 400) return app('json')->fail('短信平台:' . $res['msg']);
  175. $this->service->setConfig($this->service->account(), $data['password']);
  176. return app('json')->success('修改成功');
  177. }
  178. /**
  179. * 修改签名
  180. * @Author:Qinii
  181. * @Date: 2020/9/2
  182. * @return mixed
  183. */
  184. public function changeSign()
  185. {
  186. $data = $this->request->params(['sign','phone','code']);
  187. if(empty($data['sign'])) return app('json')->fail('签名不能为空');
  188. $res = $this->service->smsChange($data);
  189. if ($res['status'] == 400) return app('json')->fail('短信平台:' . $res['msg']);
  190. return app('json')->success('修改已提交,审核通过后自动更改');
  191. }
  192. }