Sms.php 6.1 KB

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