You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
7.3 KiB

7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use App\Common\Sms;
  4. use App\Common\Helper;
  5. use App\Common\ReturnData;
  6. use DB;
  7. use Log;
  8. //验证码
  9. class VerifyCode extends BaseModel
  10. {
  11. protected $table = 'verify_code';
  12. public $timestamps = false;
  13. protected $hidden = array();
  14. protected $guarded = array(); //$guarded包含你不想被赋值的字段数组。
  15. const STATUS_UNUSE = 0;
  16. const STATUS_USE = 1; //验证码已被使用
  17. const TYPE_GENERAL = 0; //通用
  18. const TYPE_REGISTER = 1; //用户注册业务验证码
  19. const TYPE_CHANGE_PASSWORD = 2; //密码修改业务验证码
  20. const TYPE_MOBILEE_BIND = 3; //手机绑定业务验证码
  21. const TYPE_VERIFYCODE_LOGIN = 4; //验证码登录
  22. const TYPE_CHANGE_MOBILE = 5; //修改手机号码
  23. public function getDb()
  24. {
  25. return DB::table($this->table);
  26. }
  27. /**
  28. * 列表
  29. * @param array $where 查询条件
  30. * @param string $order 排序
  31. * @param string $field 字段
  32. * @param int $offset 偏移量
  33. * @param int $limit 取多少条
  34. * @return array
  35. */
  36. public function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 10)
  37. {
  38. $model = $this->getDb();
  39. if($where){$model = $model->where($where);}
  40. $res['count'] = $model->count();
  41. $res['list'] = array();
  42. if($res['count'] > 0)
  43. {
  44. if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}}
  45. if($order){$model = parent::getOrderByData($model, $order);}
  46. if($offset){}else{$offset = 0;}
  47. if($limit){}else{$limit = 10;}
  48. $res['list'] = $model->skip($offset)->take($limit)->get();
  49. }
  50. return $res;
  51. }
  52. /**
  53. * 分页,用于前端html输出
  54. * @param array $where 查询条件
  55. * @param string $order 排序
  56. * @param string $field 字段
  57. * @param int $limit 每页几条
  58. * @param int $page 当前第几页
  59. * @return array
  60. */
  61. public function getPaginate($where = array(), $order = '', $field = '*', $limit = 10)
  62. {
  63. $res = $this->getDb();
  64. if($where){$res = $res->where($where);}
  65. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  66. if($order){$res = parent::getOrderByData($res, $order);}
  67. if($limit){}else{$limit = 10;}
  68. return $res->paginate($limit);
  69. }
  70. /**
  71. * 查询全部
  72. * @param array $where 查询条件
  73. * @param string $order 排序
  74. * @param string $field 字段
  75. * @param int $limit 取多少条
  76. * @return array
  77. */
  78. public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '')
  79. {
  80. $res = $this->getDb();
  81. if($where){$res = $res->where($where);}
  82. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  83. if($order){$res = parent::getOrderByData($res, $order);}
  84. if($offset){$res = $res->skip($offset);}
  85. if($limit){$res = $res->take($limit);}
  86. $res = $res->get();
  87. return $res;
  88. }
  89. /**
  90. * 获取一条
  91. * @param array $where 条件
  92. * @param string $field 字段
  93. * @return array
  94. */
  95. public function getOne($where, $field = '*')
  96. {
  97. $res = $this->getDb();
  98. if($where){$res = $res->where($where);}
  99. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  100. $res = $res->first();
  101. return $res;
  102. }
  103. /**
  104. * 添加
  105. * @param array $data 数据
  106. * @return int
  107. */
  108. public function add(array $data,$type = 0)
  109. {
  110. if($type==0)
  111. {
  112. // 新增单条数据并返回主键值
  113. return self::insertGetId(parent::filterTableColumn($data,$this->table));
  114. }
  115. elseif($type==1)
  116. {
  117. /**
  118. * 添加单条数据
  119. * $data = ['foo' => 'bar', 'bar' => 'foo'];
  120. * 添加多条数据
  121. * $data = [
  122. * ['foo' => 'bar', 'bar' => 'foo'],
  123. * ['foo' => 'bar1', 'bar' => 'foo1'],
  124. * ['foo' => 'bar2', 'bar' => 'foo2']
  125. * ];
  126. */
  127. return self::insert($data);
  128. }
  129. }
  130. /**
  131. * 修改
  132. * @param array $data 数据
  133. * @param array $where 条件
  134. * @return int
  135. */
  136. public function edit($data, $where = array())
  137. {
  138. $res = $this->getDb();
  139. return $res->where($where)->update(parent::filterTableColumn($data, $this->table));
  140. }
  141. /**
  142. * 删除
  143. * @param array $where 条件
  144. * @return bool
  145. */
  146. public function del($where)
  147. {
  148. $res = $this->getDb();
  149. $res = $res->where($where)->delete();
  150. return $res;
  151. }
  152. //验证码校验
  153. public static function isVerify($mobile, $code, $type)
  154. {
  155. return VerifyCode::Where('code', $code)->where('mobile', $mobile)->where('type', $type)->where('status', VerifyCode::STATUS_UNUSE)->where('expired_at', '>', date('Y-m-d H:i:s'))->first();
  156. }
  157. //生成验证码
  158. public static function getVerifyCode($mobile,$type,$text='')
  159. {
  160. //验证手机号
  161. if (!Helper::isValidMobile($mobile))
  162. {
  163. return ReturnData::create(ReturnData::MOBILE_FORMAT_FAIL);
  164. }
  165. switch ($type)
  166. {
  167. case self::TYPE_GENERAL;//通用
  168. break;
  169. case self::TYPE_REGISTER: //用户注册业务验证码
  170. break;
  171. case self::TYPE_CHANGE_PASSWORD: //密码修改业务验证码
  172. break;
  173. case self::TYPE_MOBILEE_BIND: //手机绑定业务验证码
  174. break;
  175. case self::TYPE_VERIFYCODE_LOGIN: //验证码登录
  176. break;
  177. case VerifyCode::TYPE_CHANGE_MOBILE: //修改手机号码
  178. break;
  179. default:
  180. return ReturnData::create(ReturnData::INVALID_VERIFYCODE);
  181. }
  182. $verifyCode = new VerifyCode;
  183. $verifyCode->type = $type;
  184. $verifyCode->mobile = $mobile;
  185. $verifyCode->code = rand(1000, 9999);
  186. $verifyCode->status = self::STATUS_UNUSE;
  187. //10分钟有效
  188. $verifyCode->expired_at = date('Y-m-d H:i:s',(time()+60*20));
  189. //短信发送验证码
  190. if (strpos($verifyCode->mobile, '+') !== false)
  191. {
  192. $text = "【hoo】Your DC verification Code is: {$verifyCode->code}";
  193. }
  194. else
  195. $text = "【后】您的验证码是{$verifyCode->code},有效期20分钟。";
  196. Sms::sendByYp($text,$verifyCode->mobile);
  197. $verifyCode->save();
  198. return ReturnData::create(ReturnData::SUCCESS,array('code' => $verifyCode->code));
  199. }
  200. }