legend2---开发常用语句
一、总结
一句话总结:
其实敲代码都是很类似的,比如ajax,模型等等,敲代码就是一个由生到熟的过程
1、ajax?
$.post("{:url('broadcast.index/require_broadcast')}", {need_money: need_money} ,function (data) {
页面
1 <script> 2 function require_broadcast(){ 3 $.post("{:url('broadcast.index/require_broadcast')}", {need_money: need_money} ,function (data) { 4 //console.log(data); 5 if(parseInt(data.operation_num)==4){ 6 main_content.touben_heroes=data.touben_heroes; 7 //console.log(data); 8 //更新school 9 f_mian_header.school=data.school; 10 main_content.school_time=data.school_time; 11 console.log(main_content.school_time); 12 }else{ 13 if(parseInt(data.operation_num)==3) alert('支付失败'); 14 if(parseInt(data.operation_num)==2) alert('服务器错误'); 15 if(parseInt(data.operation_num)==1) alert('连接成功'); 16 if(parseInt(data.operation_num)==0) alert('连接失败'); 17 if(parseInt(data.operation_num)==0) alert('免费刷新条件不满足'); 18 19 } 20 }); 21 } 22 </script>
控制器
1 public function require_broadcast(){ 2 $back_data=[]; 3 $back_data['operation_num']=0;//表示连接失败 4 if(request()->isAjax()){ 5 $back_data['operation_num']=1;//表示连接成功 6 7 8 //die; 9 return $back_data; 10 } 11 return $back_data; 12 }
2、事务处理?
Db::startTrans();
Db::commit();
Db::rollback();
1 //19、测试事务操作 2 public function test18(){ 3 Db::startTrans(); 4 $ans1=db('myself_goods')->delete(6); 5 $ans2=db('myself_goods')->delete(5); 6 if($ans1&&$ans2){ 7 // 提交事务 8 dump('commit'); 9 Db::commit(); 10 }else{ 11 // 回滚事务 12 Db::rollback(); 13 dump('rollback'); 14 } 15 } 16 17 //18、测试事务操作 18 public function test17(){ 19 // 启动事务 20 Db::startTrans(); 21 try{ 22 $ans1=db('myself_goods')->delete(6); 23 $ans2=db('myself_goods')->delete(7); 24 dump('$ans1: '.$ans1); 25 dump('$ans2: '.$ans2); 26 // 提交事务 27 dump('commit'); 28 Db::commit(); 29 } catch (Exception $e) { 30 // 回滚事务 31 Db::rollback(); 32 dump('rollback'); 33 } 34 }
3、页面重新加载?
location.reload();
location.reload();
4、if语句的简约代码写法?
用数组
1 //将英雄的灵根数字字符串转化为汉子数组 2 public static function reikiRootNormalize($reikiRoot){ 3 $reiki=['金','木','水','火','土','风','冰','雷','光','暗']; 4 $reikiRoot=explode(',',$reikiRoot); 5 foreach ($reikiRoot as $key => &$val){ 6 $val=$reiki[intval($val)-1]; 7 } 8 return $reikiRoot; 9 }
还有比如判断等级的
1 //计算英雄境界 2 function calcHeroGrade($grade){ 3 $gradeLevel=["练气","筑基","金丹","元婴","出窍","化神","练虚","合体","大乘","渡劫","仙人"]; 4 $gradeSmallLevel=["前期","前期","前期","中期","中期","中期","后期","后期","后期","巅峰"]; 5 $level=''; 6 $level.=$gradeLevel[intval($grade/10)]; 7 $level.=$gradeSmallLevel[intval($grade%10)]; 8 return $level; 9 }
比如判断升级灵力的
1 //$reikiGrowth:表示灵气成长 2 public static function getGradeReiki($grade,$reikiGrowth){ 3 //dump($reikiGrowth);die; 4 $rate=[1.02,1.06,1.12,1.03,1.07,1.13,1.04,1.08,1.14,1.3]; 5 $num=100*$reikiGrowth/1000; 6 $sum=0; 7 for($i=0;$i<$grade;$i++){ 8 $sum+=$num; 9 $num*=$rate[($i+10)%10]; 10 } 11 $sum+=$num; 12 $ans=[]; 13 $ans['num']=intval($num);//$grade哪一级的升级经验 14 $ans['sum']=intval($sum);//$grade那一级的总经验 15 return $ans; 16 }
5、文字存进数据库转码和解码?
转码:addslashes(htmlspecialchars($data['a_content_art1']));
解码:stripslashes(htmlspecialchars_decode($article['a_content_art1']));
转码
$data['a_content_art1']=addslashes(htmlspecialchars($data['a_content_art1']));
解码(这个不行,要一一对应)
$article['a_content_art1']=stripslashes(htmlspecialchars_decode(stripslashes($article['a_content_art1'])));
这个
$val['bq_stem_info']=stripslashes(htmlspecialchars_decode($val['bq_stem_info']));
6、正则查找和正则替换?
查找:preg_match_all($pattern, $question['bq_answer'], $keywords);
替换:preg_replace($pattern,'', $question['bq_answer_clue']);
查找
$pattern='/<h5s*?.*?>(.*?)</h5>/us'; preg_match_all($pattern, $question['bq_answer'], $keywords); if(isset($keywords[1][0])){
替换
//2.2 确定答案对应的线索:去掉关键词 $pattern='/<h5s*?.*?>(.*?)</h5>/us'; $question['bq_answer_clue']=preg_replace($pattern,'', $question['bq_answer_clue']);
7、返回ajax模板?
$back_data=[];
$back_data['operation_num']=0;
//3、鉴定装备 public function identify_equipment(){ $back_data=[]; $back_data['operation_num']=0;//表示连接失败 $back_data['operation_num_info']='';//表示返回信息 if(request()->isAjax()){ $back_data['operation_num']=1;//表示连接成功 $e_id=input('e_id');//装备id //dump($e_id); $h_id=input('h_id');//英雄id if(!intval($e_id)||!intval($e_id)){ $back_data['operation_num']=6;//没有可鉴定装备或没有可鉴定装备英雄 return $back_data; } Db::startTrans(); //1、支付灵石 $equipment=db('equipment')->alias('e')->join('school_equipment se','se.se_e_id=e.e_id','LEFT')-> join('template_equipment te','e.e_te_id=te.te_id','LEFT')->find($e_id); //dump($equipment);die; if(!isset($equipment['e_sold_price'])||!isset($equipment['se_num'])||$equipment['se_num']<1){ $back_data['operation_num']=7;//装备数量不够 return $back_data; } $need_money_num=intval($equipment['e_sold_price']/2); if($need_money_num<25) $need_money_num=25; if(!appindexmodelmoneypayAutoPay::pay($need_money_num)){ $back_data['operation_num']=3;//支付失败 return $back_data; } //2、支付活力 $base_exp=appindexmodelupgradeGetGradeReiki::getGradeReiki($equipment['te_limit_grade'])['sum']; $need_vigour_num=intval($base_exp*$equipment['e_type_grade']*30/100/3); if(!appindexmodelheropayPayVigour::pay_heroVigour($h_id,$need_vigour_num)){ $back_data['operation_num']=5;//英雄活力不够 return $back_data; } //3、鉴定装备操作:(鉴定装备,原装备数减1,如果装备数为0,就删除这条装备) $identify_ans=appindexmodelequipmentidentifyIdentifyEquipment::identify_equipment($equipment); if($identify_ans!==false){ $back_data['operation_num']=4;//操作成功 Db::commit(); }else{ $back_data['operation_num']=2;//服务器错误 Db::rollback(); } //步骤三:将英雄石信息返回到页面 $school_equipments=appindexmodelequipmentfull_infoGetSchoolEquipments::get_schoolEquipment_fullInfo(); $school=appindexmodelschoolfull_infoGetSchoolFullInfo::get_schoolFullInfo(); $back_data['school_equipments']=$school_equipments; $back_data['school']=$school; } return $back_data; }
页面
//1、鉴定装备:e_id为装备id,h_id为英雄id function identify_equipment(){ //步骤一:获取装备id和英雄id var e_id=$('#identify_equipment_e_id').val(); var h_id=$('#identify_equipment_h_id').val(); console.log(e_id); console.log(h_id); if(!parseInt(e_id)||!parseInt(h_id)) alert('没有可鉴定装备或没有可鉴定装备英雄!!'); else{ $.post("{:url('school.warehouse/identify_equipment')}", { e_id: e_id,h_id: h_id} ,function (data) { console.log(data); if(parseInt(data.operation_num)==4){ f_mian_header.school=data.school; main_content.school_equipments=data.school_equipments; //console.log(data); }else{ if(parseInt(data.operation_num)==7) alert('装备数量不够!!'); if(parseInt(data.operation_num)==6) alert('没有可鉴定装备或没有可鉴定装备英雄!!'); if(parseInt(data.operation_num)==5) alert('英雄活力不够'); if(parseInt(data.operation_num)==3) alert('灵石不够,支付失败'); if(parseInt(data.operation_num)==2) alert('服务器错误'); if(parseInt(data.operation_num)==1) alert('连接成功'); if(parseInt(data.operation_num)==0) alert('连接失败'); } }); } } //2、使用装备-->
8、layer.js使用?
直接在函数中套用提供的代码即可
<script> function layer_goods(content_id){ console.log(content_id); layer.open({ type: 0, title: [ '物品信息', ] ,content: $('#'+content_id).html() ,anim: 'up' }); } </script>