多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
需求: 1、库存低于n自动给商家发短信,通知商家进货。 2、通过切换手机号,可以控制是给老板还是给库管发短信 该功能用的是thinkcmf二开,用到了一个类库,[请点击下载](https://download.csdn.net/download/qq_33862644/10421692) - html 代码 ~~~ <div> <br/> <b>设置库存量,自动发送短信</b><br/> 发给给谁:<input type="text" name="cellphone" class="phone"/> 低于库存多少发送:<input type="text" name="cellnum" class="num"/> <input type="button" value="设置" class="set_phone" name="celle"> <a href="{:url('sms/index')}">手动測試发送短信</a> </div> ~~~ ~~~ <script> $(function () { //设置短信。点击设置按钮 $('.set_phone').click(function () { //alert('aa'); var phone = $('.phone').val(); //手機號 var num = $('.num').val(); //庫存低於多少發短信 //发送ajax $.ajax({ method: 'post', url: "{:url('Sms/add')}", data: {cellphone: phone, cellnum: num}, success: function (msg) { if (msg.code == 1) { layer.alert(msg.msg_info); } if (msg.code == 2) { layer.alert(msg.msg_info); } } }); }); }); </script> ~~~ - 配置文件 ![](https://box.kancloud.cn/1cd468967bc309f625f3f4812c99f731_760x290.png) - 控制器 代码 ~~~ <?php namespace app\ws\controller; use think\Controller; use think\Loader; use think\Config; use think\Db; class SmsController extends Controller{ //发送手机 public function index(){ header('Content-Type: text/plain; charset=utf-8'); //【查】发送给哪个手机,低于多少发送 $sms = Db::name('ws_sms')->where('status',1)->find(); //倒序,取最新的 //【查】出所有,状态 == 2 && remnant_inventory < 5 $map = [ 'status' => 2, 'remnant_inventory' => ['<',$sms['num']] ]; $info = Db::name('ws_stock')->where($map)->select(); //查數據 //循环发短信 foreach($info as $key => $value){ $res = $this->sendSms($sms['phone'],$value['brand'],$value['version'],$value['remnant_inventory']); if($res->Message == 'OK'){ echo "手动发送成功"; echo "<br />"; } if($res->Code == 'isv.BUSINESS_LIMIT_CONTROL'){ $this->success('您今天发送的短信已达上线(60条)',url('Stock/index')); } } } /** * 发送短信 * @param $phone 要发送的手机号 * @param array $data 替换模板中的变量 */ public function sendSms($phone,$goods,$version,$remnant_inventory){ Loader::import('dayu.api_demo.SmsDemo'); $config = Config::get('dayu'); $demo = new \SmsDemo( "{$config['appKey']}", //需要改的 "{$config['appSecret']}" //需要改的 ); //发送短信 $response = $demo->sendSms( "{$config['sign_name']}", // 短信签名(需要改的) "{$config['templet_code']}", // 短信模板编号(需要改的) "{$phone}", // 短信接收者(需要改的) Array( // 短信模板中变量的值(需要改的) "goods"=>"{$goods}", //品牌 "model"=>"{$version}", //型号 "num"=>"{$remnant_inventory}" //数量 ), "123" ); return $response; } /** * 【增】【改】手机号 * @return array */ public function add(){ $data = $this->request->param(); //验证数据合法不合法 $preg = '/^1\d{10}$/'; $info = preg_match($preg,$data['cellphone'],$match); //如果有$find_id走更新,无走添加 $find = DB::name('ws_sms')->where('phone','=',$data['cellphone'])->select(); $find_id = ''; //一个标识 判断用 foreach ($find as $value){ $find_id = $value['id']; } //手机号是11位 if($info && is_numeric($data['cellnum'])){ $add_res = ''; $update_res = ''; //更新 if($find_id){ //将存在的$find_id记录,更新为1,其他更新为2。 $update_a = DB::name('ws_sms')->where('id','=',$find_id)->setField('status',1); //更新数量(设置的小于多少发送) $update_c = DB::name('ws_sms')->where('id','=',$find_id)->setField('num',$data['cellnum']); $update_b = DB::name('ws_sms')->where('id','<>',$find_id)->setField('status',2); //echo DB::name('ws_sms')->getLastSql(); if($update_a && $update_b && $update_c){ $update_res = true; } }else{ $save_data = [ 'phone' => $data['cellphone'], 'num' => $data['cellnum'], 'status' => 1 ]; //添加成功1条,将该条置为1。其他的状态置为2 DB::table('cmf_ws_sms')->insert($save_data); $user_id = DB::table('cmf_ws_sms')->getLastInsID(); $add_res = DB::name('ws_sms')->where('id','<>',$user_id)->setField('status',2); } //添加成功 或 更新成功 if($add_res || $update_res){ $msg = ['code'=>1,'msg_info'=>'设置成功']; return $msg; } }else{ $msg = ['code'=>2,'msg_info'=>'设置失败,请检查手机号的位数']; return $msg; } } } ?> ~~~