Ei kuvausta

SendCode.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Http\Controllers\Code\V1;
  3. use App\Http\Controllers\Controller;
  4. use App\Model\Code\V1\Code;
  5. use App\Support\Code\Alisms;
  6. use App\Support\Code\Trsms;
  7. use App\Support\Code\Ypsms;
  8. use Illuminate\Http\Request;
  9. class SendCode extends Controller
  10. {
  11. // public function sendsCode(Request $request)
  12. // {
  13. // $errors = app('check')->NonNInt('platform_id|平台id', true)->mobile('mobile|手机号码', true)->errors;
  14. // if (!empty($errors)) {
  15. // return reValue(4002, null, $errors);
  16. // }
  17. // $id = $request->get('platform_id');
  18. // $info = Code::getCodeInfoById($id);
  19. // if (empty($info)) {
  20. // return reValue(4002, null, ['未查询到平台数据']);
  21. // }
  22. // $url = $info->url;
  23. // $type = $info->request_type;
  24. // $passParm = $info->pass_parm;
  25. // $fixedParm = $info->fixed_parm;
  26. // $passParm = json_decode($passParm, true);
  27. // $fixedParm = json_decode($fixedParm, true);
  28. // foreach ($passParm as $k => $v) {
  29. // $data[$k] = $v;
  30. // }
  31. // $must = $passParm['must'];
  32. // foreach ($must as $v) {
  33. // $tmp = $request->get($v, null);
  34. // if (!isset($tmp)) {
  35. // return reValue(4002, null, [$v . '参数必填']);
  36. // }
  37. // $data[$v] = $tmp;
  38. // }
  39. // $notMust = $passParm['not_must'];
  40. // foreach ($notMust as $v) {
  41. // $tmp = $request->get($v, null);
  42. // $data[$v] = $tmp;
  43. // }
  44. // if ($type == 1) {
  45. // $re = curlInit($url, $data, 'Post');
  46. // } else if ($type == 2) {
  47. // $re = curlInit($url, $data, 'Get');
  48. // } else {
  49. // return reValue(4002, null, ['请求方式错误']);
  50. // }
  51. // if ($re === false) {
  52. // return reValue(4003, null, ['网络错误或超时']);
  53. // }
  54. // return reValue(0, $re);
  55. // }
  56. /**
  57. * [sendCode ]
  58. * @Author mzb
  59. * @DateTime 2018-11-09T16:23:18+0800
  60. * @param Request $request [description]
  61. * @return [type] [description]
  62. */
  63. public function sendCode(Request $request)
  64. {
  65. $errors = app('check')->mobile('mobile|手机号码', true)->contain([1, 2], 'type|发送的类型', true)->must('tpl_id|模板id')->json('parameter|参数', true)->NonNInt('aisle|通道', true)->errors;
  66. if (!empty($errors)) {
  67. return reValue(4002, null, $errors);
  68. }
  69. //手机号码
  70. $mobile = $request->get('mobile');
  71. //提供商的标志位1:云片
  72. $aisle = $request->get('aisle');
  73. //json格式的参数变量
  74. $parameter = $request->get('parameter');
  75. //发送的类型1短信2语音
  76. $type = $request->get('type', 1);
  77. //模板id
  78. $tplId = $request->get('tpl_id');
  79. //发送验证码
  80. switch ($aisle) {
  81. case 1:
  82. $re = Ypsms::sendCode($tplId, $mobile, $parameter, $type);
  83. $aisleName = '云片';
  84. break;
  85. case 2:
  86. $re = Trsms::sendCode($tplId, $mobile, $parameter, $type);
  87. $aisleName = '天瑞';
  88. break;
  89. case 3:
  90. $re = Alisms::sendCode($tplId, $mobile, $parameter, $type);
  91. $aisleName = '阿里';
  92. // print_r($re);exit;
  93. break;
  94. }
  95. if (!isset($re)) {
  96. return reValue(4002, null, ['通道不存在']);
  97. }
  98. if ($re === false) {
  99. return reValue(4003, null, ['网络错误或超时']);
  100. }
  101. //计费字段发送成功必有否则发送失败
  102. if ($re['IS_SUCCESS'] == 1) {
  103. $appKey = $request->header('appkey');
  104. Code::addStatistics($mobile, $aisleName, $appKey);
  105. return reValue(0, $re);
  106. }else{
  107. //liujuan error日志表
  108. $appKey = $request->header('appkey');
  109. $reason=$re['reason'];
  110. Code::addStatistics_error($mobile, $aisleName, $appKey, $reason);
  111. return reValue(4003, $re);
  112. }
  113. // 原版return reValue(4003, $re);
  114. }
  115. /**
  116. * [sendCodes 群发短信]
  117. * @Author mzb
  118. * @DateTime 2018-11-09T16:20:01+0800
  119. * @param Request $request [description]
  120. * @return [type] [description]
  121. */
  122. public function sendCodes(Request $request)
  123. {
  124. $errors = app('check')->must('mobiles|手机号码')->NonNInt('aisle|通道', true)->must('tpl_id|模板id')->json('parameter|参数', true)->errors;
  125. if (!empty($errors)) {
  126. return reValue(4002, null, $errors);
  127. }
  128. //手机号码
  129. $mobiles = $request->get('mobiles');
  130. //json格式的参数变量
  131. $parameter = $request->get('parameter');
  132. //模板id
  133. $tplId = $request->get('tpl_id');
  134. //提供商的标志位1:云片
  135. $aisle = $request->get('aisle');
  136. //发送验证码
  137. switch ($aisle) {
  138. case 1:
  139. $re = Ypsms::sendCodes($tplId, $mobiles, $parameter);
  140. $aisleName = '云片';
  141. break;
  142. case 2:
  143. $re = Trsms::sendCodes($tplId, $mobiles, $parameter);
  144. $aisleName = '天瑞';
  145. break;
  146. case 3:
  147. $re = Alisms::sendCodes($tplId, $mobiles, $parameter);
  148. $aisleName = '阿里';
  149. break;
  150. }
  151. if (!isset($re)) {
  152. return reValue(4002, null, ['通道不存在']);
  153. }
  154. if ($re === false) {
  155. return reValue(4003, null, ['网络错误或超时']);
  156. }
  157. //计费字段发送成功必有否则发送失败
  158. if ($re['IS_SUCCESS'] == 1) {
  159. $appKey = $request->header('appkey');
  160. $mobiles = explode(',', $mobiles);
  161. //循环插入效率很差需要优化
  162. foreach ($mobiles as $mobile) {
  163. Code::addStatistics($mobile, $aisleName, $appKey);
  164. }
  165. return reValue(0, $re);
  166. }else{
  167. //liujuan error日志表
  168. $appKey = $request->header('appkey');
  169. $mobiles = explode(',', $mobiles);
  170. $reason=$re['reason'];
  171. foreach ($mobiles as $mobile) {
  172. Code::addStatistics_error($mobile, $aisleName, $appKey, $reason);
  173. }
  174. return reValue(4003, $re);
  175. }
  176. // 原版
  177. // return reValue(4003, $re);
  178. }
  179. /**
  180. * [返回所有通道 ]
  181. * @Author zxr
  182. * @DateTime 2019-01-21T11:23:18+0800
  183. * @return [type] [description]
  184. */
  185. public function returnSmsChannel()
  186. {
  187. $sms_channels = Code::getSmsChannel();
  188. return reValue(0, $sms_channels);
  189. }
  190. /**
  191. * [根据通道id返回签名和模板 ]
  192. * @Author zxr
  193. * @DateTime 2019-01-21T11:23:18+0800
  194. * @return [type] [description]
  195. */
  196. public function returnSignAndTpl(Request $request)
  197. {
  198. $sms_channel_id = $request->input('sms_channel_id');
  199. $sms_sign_tpl = Code::getSignAndTpl($sms_channel_id);
  200. return reValue(0, $sms_sign_tpl);
  201. }
  202. }