Apply.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\api\model\supplier;
  3. use app\common\model\settings\Setting;
  4. use app\common\model\supplier\Apply as ApplyModel;
  5. use app\common\model\user\User as UserModel;
  6. use app\common\model\user\Sms as SmsModel;
  7. use app\api\model\supplier\Category as CategoryModel;
  8. /**
  9. * 供应商申请模型类
  10. */
  11. class Apply extends ApplyModel
  12. {
  13. /**
  14. * 添加
  15. */
  16. public function add($data)
  17. {
  18. // 开启事务
  19. $this->startTrans();
  20. try {
  21. //是否需要短信验证
  22. $sms_open = Setting::getItem('store')['sms_open'];
  23. if($sms_open == 1){
  24. $code = $this->verifyCode($data);
  25. if (!$code) {
  26. $this->error = '验证码错误或失效';
  27. return false;
  28. }
  29. }
  30. $num = $this->getStoreName($data['store_name']);
  31. if ($num > 0) {
  32. $this->error = '店铺名已存在';
  33. return false;
  34. }
  35. $isApply = $this->isApply($data['user_id']);
  36. if ($isApply > 0) {
  37. $this->error = '已经申请开店';
  38. return false;
  39. }
  40. $mobile = $this->getMobile($data['mobile']);
  41. if ($mobile > 0) {
  42. $this->error = '手机号码已存在';
  43. return false;
  44. }
  45. //获取保证金
  46. $CategoryInfo = CategoryModel::detail($data['category_id']);
  47. $data['deposit_money'] = $CategoryInfo['deposit_money'];
  48. // 添加供应商
  49. $data['password'] = salt_hash($data['password']);
  50. $this->save($data);
  51. // 更改用户为供应商
  52. UserModel::updateType($data['user_id'], 2);
  53. $this->commit();
  54. return true;
  55. } catch (\Exception $e) {
  56. $this->error = $e->getMessage();
  57. $this->rollback();
  58. return false;
  59. }
  60. }
  61. //验证验证码
  62. public function verifyCode($data)
  63. {
  64. $model = new SmsModel();
  65. $code = $model->where('mobile', '=', $data['mobile'])->order('sms_id desc')->where('create_time','>',time()-10*60)->value('code');
  66. if($code&&$code==$data['code']){
  67. return true;
  68. }else{
  69. return false;
  70. }
  71. }
  72. //判断店铺名
  73. public function getStoreName($store_name)
  74. {
  75. return $this->where('store_name', '=', $store_name)->where('status','in','0,1,3')->count();
  76. }
  77. //判断用户是否申请
  78. public function isApply($user_id){
  79. return $this->where('user_id', '=', $user_id)->where('status','in','0,1,3')->count();
  80. }
  81. //判断手机号是否存在
  82. public function getMobile($mobile){
  83. return $this->where('mobile', '=', $mobile)->where('status','in','0,1,3')->count();
  84. }
  85. }