common.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. use think\facade\Db;
  3. // 这是系统自动生成的公共文件
  4. function make_verify(){
  5. $code = rand(100000,999999);
  6. return $code;
  7. }
  8. /**
  9. * 发送短信接口
  10. * @param type $phone
  11. * @param type $code
  12. */
  13. function sendMessage($phone, $code) {
  14. //1.下列选项必填
  15. $url = 'https://rtcsms.cn-north-1.myhuaweicloud.com:10743/sms/batchSendSms/v1'; //APP接入地址+接口访问URI
  16. $APP_KEY = 'ww3mKZEWh3fhboNZ791pL5fhSNfJ'; //APP_Key
  17. $APP_SECRET = 'nvXKrQQeEkQRp5750M2p85ILs4xC'; //APP_Secret
  18. $sender = '99200620888880002777'; //国内短信签名通道号或国际/港澳台短信通道号
  19. $TEMPLATE_ID = '54b09b74ee764cca90571b65bfb20f9f'; //模板ID
  20. //$sender = '8821032432899'; //国内短信签名通道号或国际/港澳台短信通道号
  21. //$TEMPLATE_ID = 'faa13c0b2deb4646a31304434c07b856'; //模板ID
  22. $signature = '泰康广源'; //签名名称
  23. //必填,全局号码格式(包含国家码),示例:+8615123456789,多个号码之间用英文逗号分隔
  24. $receiver = '+86'.$phone; //短信接收人号码
  25. //选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告
  26. $statusCallback = '';
  27. /**
  28. * 选填,使用无变量模板时请赋空值 $TEMPLATE_PARAS = '';
  29. * 单变量模板示例:模板内容为"您的验证码是${NUM_6}"时,$TEMPLATE_PARAS可填写为'["369751"]'
  30. * 双变量模板示例:模板内容为"您有${NUM_2}件快递请到${TXT_32}领取"时,$TEMPLATE_PARAS可填写为'["3","人民公园正门"]'
  31. * 查看更多模板变量规则:常见问题>业务规则>短信模板内容审核标准
  32. * @var string $TEMPLATE_PARAS
  33. */
  34. $TEMPLATE_PARAS = '["'.$code.'"]'; //模板变量,根据自身使用的模板,其值长度和个数与模板对应
  35. //请求Headers
  36. $headers = [
  37. 'Content-Type: application/x-www-form-urlencoded',
  38. 'Authorization: WSSE realm="SDP",profile="UsernameToken",type="Appkey"',
  39. 'X-WSSE: '.buildWsseHeader($APP_KEY, $APP_SECRET)
  40. ];
  41. //请求Body
  42. $data = http_build_query([
  43. 'from' => $sender,
  44. 'to' => $receiver,
  45. 'templateId' => $TEMPLATE_ID,
  46. 'templateParas' => $TEMPLATE_PARAS,
  47. 'statusCallback' => $statusCallback,
  48. 'signature' => $signature //使用国内短信通用模板时,必须填写签名名称
  49. ]);
  50. $ch = curl_init();
  51. curl_setopt ($ch, CURLOPT_URL, $url);
  52. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  53. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  54. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  55. curl_setopt($ch, CURLOPT_POST, true);
  56. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  57. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  58. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
  59. $response = curl_exec($ch);
  60. return $response;
  61. }
  62. /**
  63. * 构造X-WSSE参数值
  64. * @param string $appKey
  65. * @param string $appSecret
  66. * @return string
  67. */
  68. function buildWsseHeader($appKey, $appSecret)
  69. {
  70. date_default_timezone_set('Asia/Shanghai');
  71. $now = date('Y-m-d\TH:i:s\Z'); //Created
  72. $nonce = uniqid(); //Nonce
  73. $base64 = base64_encode(hash('sha256', ($nonce . $now . $appSecret))); //PasswordDigest
  74. return sprintf("UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\"",
  75. $appKey, $base64, $nonce, $now);
  76. }
  77. /**手机号验证
  78. * @param $mobile
  79. * @return bool
  80. */
  81. function checkMobile($mobile){
  82. if (!is_numeric($mobile)) {
  83. return false;
  84. }
  85. return preg_match('#^1[3,4,5,6,7,8,9]{1}[\d]{9}$#', $mobile) ? true : false;
  86. }
  87. /**邮箱验证
  88. * @param $email
  89. * @return bool
  90. */
  91. function checkEmail($email){
  92. if (!$email) {
  93. return false;
  94. }
  95. return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false;
  96. }
  97. /**
  98. * @param
  99. * @return int
  100. */
  101. function makeSalt(){
  102. $salt = rand(10000000,99999999);
  103. return $salt;
  104. }
  105. /**
  106. * @param $account
  107. * @param float|int $expire
  108. * @return string
  109. */
  110. function makeToken($account,$expire=0){
  111. $tokeninfo = Db::table("sys_token")->where(['accountid'=>$account['id']])->find();
  112. $date = date("Y-m-d H:i:s");
  113. $token = sha1($account['username'].$account['mobile'].$date);
  114. if(!$tokeninfo){
  115. $data=['token'=>$token,"accountid"=>$account['id'],"addtime"=>$date,"expire"=>$expire];
  116. }else{
  117. $data=$tokeninfo;
  118. $data['token']=$token;
  119. $data['addtime']=$date;
  120. $data['expire']=$expire;
  121. }
  122. $resulr=Db::table("sys_token")->save($data);
  123. return $resulr? $token:"";
  124. }
  125. /**
  126. * @param $token
  127. * @return array
  128. * @throws \think\db\exception\DataNotFoundException
  129. * @throws \think\db\exception\DbException
  130. * @throws \think\db\exception\ModelNotFoundException
  131. * @throws \think\exception\DbException
  132. */
  133. function VerifyToken($token){
  134. $tokeninfo = Db::table("sys_token")->where(['token'=>$token])->find();
  135. if(!$tokeninfo){
  136. return ['code'=>101,"message"=>'token不存在'];
  137. }
  138. if($tokeninfo['expire']<time()){
  139. return ['code'=>102,"message"=>'token已失效'];
  140. }
  141. $acc = Db::name("view_userinfo")->where("id","=",$tokeninfo['accountid'])->find();
  142. if(!$acc){
  143. return ['code'=>103,"message"=>'未找到对应的账户'];
  144. }
  145. $verify=sha1($acc['username'].$acc['mobile'].$tokeninfo['addtime']);
  146. if($verify!=$token){
  147. return ['code'=>104,"message"=>'token无效'];
  148. }
  149. $tokeninfo['expire'] = time()+1800;
  150. Db::table("sys_token")->save($tokeninfo);
  151. return ['code'=>0,"message"=>'验证通过',"user"=>['id'=>$tokeninfo['accountid']]];
  152. }
  153. /**
  154. * POST 请求
  155. * @param string $url
  156. * @param array $param
  157. * @param boolean $post_file 是否文件上传
  158. * @return string content
  159. */
  160. function post($url,$data,$header=[])
  161. {
  162. //对空格进行转义
  163. $url = str_replace(' ','+',$url);
  164. $ch = curl_init();
  165. //设置选项,包括URL
  166. curl_setopt($ch, CURLOPT_URL, "$url");
  167. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  168. curl_setopt($ch, CURLOPT_HEADER, 0);
  169. curl_setopt($ch,CURLOPT_TIMEOUT,3); //定义超时3秒钟
  170. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  171. // POST数据
  172. // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  173. curl_setopt($ch, CURLOPT_POST, 1);
  174. // 把post的变量加上
  175. curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //所需传的数组用http_bulid_query()函数处理一下,就ok了
  176. curl_setopt($ch, CURLOPT_HEADER, true);
  177. //执行并获取url地址的内容
  178. $output = curl_exec($ch);
  179. $errorCode = curl_errno($ch);
  180. //释放curl句柄
  181. $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  182. // 根据头大小去获取头信息内容
  183. $header = substr($output, 0, $headerSize);
  184. curl_close($ch);
  185. if(0 !== $errorCode) {
  186. return false;
  187. }
  188. return $header;
  189. }
  190. function post2($url,$data,$header=[])
  191. {
  192. //对空格进行转义
  193. $url = str_replace(' ','+',$url);
  194. $ch = curl_init();
  195. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  196. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  197. //设置选项,包括URL
  198. curl_setopt($ch, CURLOPT_URL, "$url");
  199. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  200. curl_setopt($ch, CURLOPT_HEADER, 0);
  201. // POST数据
  202. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  203. curl_setopt($ch, CURLOPT_POST, 1);
  204. // 把post的变量加上
  205. curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //所需传的数组用http_bulid_query()函数处理一下,就ok了
  206. $output = curl_exec($ch);
  207. $errorCode = curl_errno($ch);
  208. curl_close($ch);
  209. if(0 !== $errorCode) {
  210. return false;
  211. }
  212. return $output;
  213. }