Sms.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace mall\basic;
  10. use think\facade\Db;
  11. class Sms {
  12. public static function send($data=[],$type=""){
  13. if(empty($data["mobile"])){
  14. throw new \Exception("手机号码不能为空");
  15. }
  16. Db::startTrans();
  17. try{
  18. $config = Db::name("sms_template")->where("sign",$type)->find();
  19. if(empty($config)){
  20. throw new \Exception("短信模板不存在");
  21. }else if($config["status"] == 1){
  22. throw new \Exception("当前短信模板已被禁用");
  23. }
  24. switch($type){
  25. case "register":
  26. case "repassword":
  27. $num = mt_rand(1000,9999);
  28. $str = str_replace('${code}',$num,$config["template_param"]);
  29. Db::name("users_sms")->insert([
  30. "mobile"=>$data["mobile"],
  31. "code"=>$num,
  32. "create_time"=>time()
  33. ]);
  34. break;
  35. case "payment_success":
  36. case "deliver_goods":
  37. if(empty($data['order_no'])){
  38. throw new \Exception("订单号不能为空");
  39. }
  40. $str = str_replace('${order_no}',$data['order_no'],$config["template_param"]);
  41. break;
  42. }
  43. \mall\library\sms\Sms::send([
  44. "PhoneNumbers"=>$data["mobile"],
  45. "SignName"=>$config["sign_name"],
  46. "TemplateCode"=>$config["template_code"],
  47. "TemplateParam"=>$str
  48. ]);
  49. Db::commit();
  50. }catch (\Exception $e){
  51. Db::rollback();
  52. throw new \Exception($e->getMessage(),$e->getCode());
  53. }
  54. return true;
  55. }
  56. }