Sms.php 6.0 KB

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